// const { axios } = require("../../../../../fitness-demo/src/lib/axios.min");

//引入当前页面的 less文件 
require("../less/login.less")
// require("../fonts/iconfont.css")
// require("../fonts.iconfont.js")
document.ready(function () {
    let phone = document.querySelector("#phone");
    let psd = document.querySelector("#psd");
    let onbtn = document.querySelector("#onbtn");
    let box = document.querySelector("#box");

    onbtn.addEventListener("click", function () {
        // console.log(111);
        // console.log(axios);
        if (!phone.value) {
            box.innerHTML = "手机号不能为空";
            return;
        }
        if (!window.$util.phoneCheck(phone.value)) {
            box.innerHTML = "请输入正确的手机号码";
            phone.focus();
            return;
        }
        if (!psd.value) {
            box.innerHTML = "请输入密码";
            return;
        }
        if (!window.$util.passwordCheck(psd.value)) {
            box.innerHTML = "密码格式不正确";
            psd.focus();
            return;
        }
        box.innerHTML = "";
        //先检查账号是否存在
        axios.post("http://139.9.177.51:3701/api/user/isExist", {
            "account": phone.value,
            "password": psd.value
        }).then(res => {
            //如果不存在
            if (res.data.errno === 0) {
                window.$util.popupBox("jieshu", "请先注册");
            } else if (res.data.errno === 10002) {
                //如果存在
                axios.post("http://139.9.177.51:3701/api/user/login", {
                    "account": phone.value,
                    "password": psd.value
                }).then(res => {
                    if (res.data.errno === 20002) {
                        window.$util.popupBox("jieshu", res.data.message);
                    } else if (res.data.errno === 0) {
                        window.sessionStorage.setItem("user", JSON.stringify(res.data.data) )
                        window.$util.popupBox("gougou", "登录成功")
                        setTimeout(function () {
                             location.href="index.html";
                        }, 1000)

                    }
                }).catch(error => {
                    console.log(error);
                })
            }
        }).catch(error => {
            console.log(error);
        })
        box.innerHTML = "";

    })


})
